package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.piaohao.model.OrgTree;
import org.piaohao.model.PosOrgTree;
import org.piaohao.model.Position;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-4, 17:23:43
 *
 * @author piaohao
 */
public class PosController extends Controller {

    public static final String moduleName = "岗位模块";

    public void add() {
        try {
            Position pos = getModel(Position.class);
            String orgTreeId = getPara("p_id");
            Record oot = Db.findFirst("select * from t_dep_org_tree where fk_org_tree_id=?", orgTreeId);
            String depId = oot.getStr("fk_dep_id");
            pos.set("dep_id", depId);
            String id1 = UUIDGenerator.getUUID();
            if (pos.getStr("id") == null || pos.getStr("id").trim().equals("")) {
                pos.set("id", id1);
                pos.save();
            }
            OrgTree ot = new OrgTree();
            String id2 = UUIDGenerator.getUUID();
            ot.set("id", id2);
            ot.set("p_id", orgTreeId);
            ot.set("text", pos.getStr("name"));
            ot.set("leaf", true);
            ot.set("type", "岗位");
            ot.set("iconCls", "position");
            ot.save();

            PosOrgTree pot2 = new PosOrgTree();
            String id3 = UUIDGenerator.getUUID();
            pot2.set("id", id3);
            pot2.set("fk_pos_id", id1);
            pot2.set("fk_org_tree_id", id2);
            pot2.save();
            Map extMap = new HashMap();
            extMap.put("id", id2);
            renderJson(JsonUtil.printSuccess(true, "成功", extMap));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            Position pos = getModel(Position.class);
            String posId = Db.queryStr("select fk_pos_id from t_pos_org_tree where fk_org_tree_id=?", getPara("org_tree_id"));
            pos.set("id", posId);
            pos.update();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void delete() {
        try {
            String orgTreeId = getPara("id");
            String posId = Db.queryStr("select fk_pos_id from t_pos_org_tree where fk_org_tree_id=?", orgTreeId);
            String depId = Db.queryStr("select dep_id from t_position where id=?", posId);
            Db.update("delete from t_pos_org_tree where fk_org_tree_id=?", orgTreeId);
            Position.dao.deleteById(posId);
            OrgTree.dao.deleteById(orgTreeId);
            Db.update("update t_employee set pos_id=? where dep_id=?", null, depId);
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void getAll() {
        try {
            List<Position> deps = Position.dao.find("select name,id from t_position where dep_id=?", getPara(0));
            renderJson(deps);
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void showDetail() {
        try {
            String id = getPara(0);
            Record pos = Db.findFirst("select p.* from t_position p left join t_pos_org_tree pot on p.id=pot.fk_pos_id where pot.fk_org_tree_id=?", id);
            setAttr("pos", pos);
            render("/pages/organization/posDetail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }
}
